import 'dart:io';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:intl/intl.dart';
import '../models/complaint.dart';
import '../widgets/image_viewer.dart';
import '../widgets/video_player_widget.dart';

class ComplaintDetailPage extends StatelessWidget {
  final Complaint complaint;

  const ComplaintDetailPage({super.key, required this.complaint});

  String _getTypeText(ComplaintType type) {
    switch (type) {
      case ComplaintType.air:
        return '大气污染';
      case ComplaintType.water:
        return '水污染';
      case ComplaintType.soil:
        return '土壤污染';
    }
  }

  Future<void> _callPhone(String phone) async {
    final Uri phoneUri = Uri(scheme: 'tel', path: phone);
    if (await canLaunchUrl(phoneUri)) {
      await launchUrl(phoneUri);
    }
  }

  void _showImage(BuildContext context) {
    if (complaint.imagePath != null) {
      Get.to(() => ImageViewer(imagePath: complaint.imagePath!));
    }
  }

  void _playVideo(BuildContext context) {
    if (complaint.videoPath != null) {
      Get.to(() => VideoPlayerWidget(videoPath: complaint.videoPath!));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('投诉详情')),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Chip(
                          label: Text(_getTypeText(complaint.type)),
                          backgroundColor: Theme.of(
                            context,
                          ).primaryColor.withOpacity(0.1),
                        ),
                        Text(
                          DateFormat(
                            'yyyy-MM-dd HH:mm',
                          ).format(complaint.createdAt),
                          style: Theme.of(context).textTheme.bodySmall,
                        ),
                      ],
                    ),
                    const SizedBox(height: 16),
                    const Text(
                      '投诉人信息',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const SizedBox(height: 8),
                    ListTile(
                      leading: const Icon(Icons.phone),
                      title: const Text('联系电话'),
                      subtitle: Text(complaint.phone),
                      trailing: IconButton(
                        icon: const Icon(Icons.phone),
                        onPressed: () => _callPhone(complaint.phone),
                      ),
                    ),
                    const Divider(),
                    const Text(
                      '问题描述',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const SizedBox(height: 8),
                    Text(complaint.description),
                    const SizedBox(height: 16),
                    const Text(
                      '位置信息',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const SizedBox(height: 8),
                    ListTile(
                      leading: const Icon(Icons.location_on),
                      title: const Text('地址'),
                      subtitle: Text(complaint.address),
                    ),
                    const SizedBox(height: 8),
                    Text('纬度：${complaint.latitude} 经度：${complaint.longitude}'),
                    if (complaint.imagePath != null) ...[
                      const SizedBox(height: 16),
                      const Text(
                        '现场照片',
                        style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      const SizedBox(height: 8),
                      GestureDetector(
                        onTap: () => _showImage(context),
                        child: Container(
                          height: 200,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(8),
                            image: DecorationImage(
                              image: FileImage(File(complaint.imagePath!)),
                              fit: BoxFit.cover,
                            ),
                          ),
                        ),
                      ),
                    ],
                    if (complaint.videoPath != null) ...[
                      const SizedBox(height: 16),
                      const Text(
                        '现场视频',
                        style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      const SizedBox(height: 8),
                      GestureDetector(
                        onTap: () => _playVideo(context),
                        child: Container(
                          height: 200,
                          decoration: BoxDecoration(
                            color: Colors.black,
                            borderRadius: BorderRadius.circular(8),
                          ),
                          child: const Center(
                            child: Icon(
                              Icons.play_circle_filled,
                              color: Colors.white,
                              size: 48,
                            ),
                          ),
                        ),
                      ),
                    ],
                    if (complaint.isProcessed &&
                        complaint.adminResponse != null) ...[
                      const SizedBox(height: 16),
                      const Text(
                        '处理结果',
                        style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      const SizedBox(height: 8),
                      Container(
                        padding: const EdgeInsets.all(16),
                        decoration: BoxDecoration(
                          color: Colors.green.withOpacity(0.1),
                          borderRadius: BorderRadius.circular(8),
                        ),
                        child: Text(
                          complaint.adminResponse!,
                          style: const TextStyle(color: Colors.green),
                        ),
                      ),
                    ],
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
